Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start with python match statement #1801

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Start with python match statement #1801

wants to merge 18 commits into from

Conversation

KuechA
Copy link
Contributor

@KuechA KuechA commented Oct 21, 2024

This PR adds the basic comparisons which can be found in python's match statement. The statement is modeled as a SwitchStatement and the main issue are the CaseStatements which offer a wide range of checks, some of which we cannot model trivially.

Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 84.26966% with 14 lines in your changes missing coverage. Please review.

Project coverage is 75.30%. Comparing base (47d6f94) to head (631fdb3).

Files with missing lines Patch % Lines
...fer/aisec/cpg/frontends/python/StatementHandler.kt 81.42% 7 Missing and 6 partials ⚠️
...er/aisec/cpg/frontends/python/ExpressionHandler.kt 94.44% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
...ofer/aisec/cpg/graph/statements/SwitchStatement.kt 72.00% <ø> (ø)
...de/fraunhofer/aisec/cpg/frontends/python/Python.kt 83.38% <100.00%> (+8.77%) ⬆️
...er/aisec/cpg/frontends/python/ExpressionHandler.kt 75.27% <94.44%> (+0.18%) ⬆️
...fer/aisec/cpg/frontends/python/StatementHandler.kt 84.21% <81.42%> (+0.14%) ⬆️

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@KuechA KuechA enabled auto-merge (squash) October 31, 2024 10:06
Copy link
Contributor

@maximiliankaul maximiliankaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I like this very much.
Some more nitpicking comments, but I think this is good to be merged :)
We should open an issue to track the not implemented cases...

* [Python.AST.match_case.body].
*
* The [CaseStatement] is generated by the [Python.AST.match_case.pattern] and, if available,
* [Python.AST.match_case.guard]. If there's a `guard` present, we model the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"If there's a guard present, we model the... " this is a bit confusing. There is also a CaseStmt if there is no guard, isn't there?

} else {
newCaseStatement(rawNode = node).apply {
this.caseExpression =
node.guard?.let {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this style. I think a simple if / else if / else at the top level would be easier to read.

newBinaryOperator(operatorCode = "===", rawNode = node).implicit().apply {
this.lhs = newReference(name = subject)
this.rhs =
when (val value = node.value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this should be an extra function (at least if we use this logic somewhere else, too). Have you seen easyConstant in the ExpressionHandler?.
We only expect True, False or None here according to the doc.

}

@Test
fun testMatchAnd() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testMatchGuard?

KuechA and others added 2 commits November 14, 2024 10:26
…ontends/python/StatementHandler.kt

Co-authored-by: Maximilian Kaul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants